---
title: Shader
---

```glsl
Shader "ShaderName" {
  ...
  // Global variables
  ...
  SubShader "SubShaderName" {
    ...
  }
  ...
}
```

In ShaderLab, `Shader` is a collection of shader programs and other engine rendering settings in the traditional rendering pipeline. It allows defining multiple shader programs within the same `Shader` object and instructs Galacean on how to choose and use them during rendering. The `Shader` object has a nested structure, corresponding to the engine's encapsulated [Shader](/apis/galacean/#Shader), [SubShader](/apis/galacean/#SubShader), and [ShaderPass](/apis/galacean/#ShaderPass) objects.

## Material Property Definition

In ShaderLab, developers can customize the Inspector property panel for custom materials using this Shader through the [`Editor`](./editor/) directive.

## Global Variables

You can declare 4 types of global variables in ShaderLab: RenderState, Structs, Functions, and Single Variables.

### RenderState

Includes BlendState, DepthState, StencilState, RasterState

- BlendState

  ```glsl
  BlendState {
    Enabled[n]: bool;
    ColorBlendOperation[n]: BlendOperation;
    AlphaBlendOperation[n]: BlendOperation;
    SourceColorBlendFactor[n]: BlendFactor;
    SourceAlphaBlendFactor[n]: BlendFactor;
    DestinationColorBlendFactor[n]: BlendFactor;
    DestinationAlphaBlendFactor[n]: BlendFactor;
    ColorWriteMask[n]: float // 0xffffffff
    BlendColor: vec4;
    AlphaToCoverage: bool;
  }
  ```

  [n] can be omitted. When using MRT, [n] specifies a particular MRT render state. Omitting it sets all MRT states. BlendOperation and BlendFactor enums are the same as the engine API.

- DepthState

  ```glsl
  DepthState {
    Enabled: bool;
    WriteEnabled: bool;
    CompareFunction: CompareFunction;
  }
  ```

  CompareFunction enums are the same as the engine API.

- StencilState

  ```glsl
  StencilState {
    Enabled: bool;
    ReferenceValue: int;
    Mask: float; // 0xffffffff
    WriteMask: float; // 0xffffffff
    CompareFunctionFront: CompareFunction;
    CompareFunctionBack: CompareFunction;
    PassOperationFront: StencilOperation;
    PassOperationBack: StencilOperation;
    FailOperationFront: StencilOperation;
    FailOperationBack: StencilOperation;
    ZFailOperationFront: StencilOperation;
    ZFailOperationBack: StencilOperation;
  }
  ```

  CompareFunction and StencilOperation enums are the same as the engine API.

- RasterState

  ```glsl
  RasterState {
    CullMode: CullMode;
    DepthBias: float;
    SlopeScaledDepthBias: float;
  }
  ```

  CullMode enums are the same as the engine API.

Example of setting `BlendState` in `ShaderLab`:

```glsl
Shader "Demo" {
  ...
  BlendState customBlendState
  {
    Enabled = true;
    // Constant variable assignment
    SourceColorBlendFactor = BlendFactor.SourceColor;
    // Variable assignment
    DestinationColorBlendFactor = material_DstBlend;
  }
  ...
  Pass "0" {
    ...
    BlendState = customBlendState;
    ...
  }
}
```

The above example shows two ways of assigning values to BlendState properties: _constant assignment_ and _variable assignment_:

- Constant assignment means the right side of the assignment statement is a specified engine enum variable, e.g., BlendFactor.SourceColor.
- Variable assignment means the right side of the assignment statement is any variable name. The specific value of the variable is specified by the user at runtime through the ShaderData.setInt("material_DstBlend", BlendFactor.SourceColor) API.

### Structs, Functions

Same as the syntax in GLSL.

### Single Variables

```glsl
[lowp/mediump/highp] variableType variableName;
```

Similar to other programming languages, global variables in ShaderLab also have scope and name overriding principles. In simple terms, the scope of global variables in ShaderLab is limited to the SubShader or Pass module in which they are declared. The name overriding principle means that if there are global variables with the same name within a Pass, the global variables within the Pass will override the global variables with the same name in the SubShader.
